/* 
 * validate form验证
 * 
 */
$().ready(function() {
	var baseUrl = 'http://kf.rmhospital.com/';
	//添加就诊人
	$("#userAddForm").validate({
		rules: {
			trueName: {
				required: true,
			},
			birthday: {
				required: true,
			},
			idNumber: {
				required: true,
				minlength: 18,
				maxlength: 18
			},	
            mobilePhone: {
				required: true,
			},			
		},
		messages: {
			trueName: {
				required: '请填写姓名',
			},
			birthday: {
				required: "请选择生日",		
			},
			idNumber: {
				required: "请填写正确身份证号",
				minlength: "请填写正确身份证号",
				maxlength: "请填写正确身份证号"
			},
            mobilePhone: {
				required: "请填写联系电话",
			},				
		},
		submitHandler:function(form) {
          var url = baseUrl+'member/userAdd';		  
          $.ajax({
  			url:url,
			type:'post',
			dataType:'json',
			data: $('#userAddForm').serialize(),
			success:function(res){
				if(res.status==1){
			     	top.location.href = baseUrl+res.url;
				}else{
					layer.alert(res.info, {icon: 2});
				}
			}
          })
		}		
	});	
	
	//修改密码
	$("#epwdFrom").validate({
		rules: {
			oldPassword: {
				required: true,
			},
			newPassword: {
				required: true,
				minlength: 6,
			},
			newCkPassword: {
				required: true,
				minlength: 6,
				equalTo:"#newPassword",
			},			
		},
		messages: {
			oldPassword: {
				required: "请输入原密码",
			},
			newPassword: {
				required: "请输入密码",
				minlength: "密码必须为6数以上",
			},
			newCkPassword: {
				required: "请输入确认密码",
				minlength: "密码必须为6数以上",
				equalTo:"密码与确认密码不一致",
			},		
		},
		submitHandler:function(form) {
          var url = baseUrl+'member/epwd';		  
          $.ajax({
  			url:url,
			type:'post',
			dataType:'json',
			data: $('#epwdFrom').serialize(),
			success:function(res){
				if(res.status==1){
			     	top.location.href = baseUrl+res.url;
				}else{
					layer.alert(res.info, {icon: 2});
				}
			}
          })
		}		
		
	});

	//修改就诊人信息
	$("#ueform").validate({
		rules: {
			trueName: {
				required: true,
			},
			birthday: {
				required: true,
			},
			idNumber: {
				required: true,
				minlength: 18,
				maxlength: 18
			},	
            mobilePhone: {
				required: true,
			},			
		},
		messages: {
			trueName: {
				required: '请填写姓名',
			},
			birthday: {
				required: "请选择生日",		
			},
			idNumber: {
				required: "请填写正确身份证号",
				minlength: "请填写正确身份证号",
				maxlength: "请填写正确身份证号"
			},
            mobilePhone: {
				required: "请填写联系电话",
			},				
		},
		submitHandler:function(form) {
          var url = baseUrl+'member/userEdit';		  
          $.ajax({
  			url:url,
			type:'post',
			dataType:'json',
			data: $('#ueform').serialize(),
			success:function(res){
				if(res.status==1){
			     	top.location.href = baseUrl+res.url;
				}else{
					layer.alert(res.info, {icon: 2});
				}
			}
          })
		}	
	});
	
	//绑定就诊卡
	$("#cardAddForm").validate({
		rules: {
			cardNum: {
				required: true,
			},			
		},
		messages: {
			cardNum: {
				required: "请填写就诊卡",
			},	
		},
		submitHandler:function(form) {
          var url = baseUrl+'member/cardAdd';		  
          $.ajax({
  			url:url,
			type:'post',
			dataType:'json',
			data: $('#cardAddForm').serialize(),
			success:function(res){
				if(res.status==1){
			     	top.location.href = baseUrl+res.url;
				}else{
					layer.alert(res.info, {icon: 2});
				}
			}
          })
		}	
	});	
	

	
	
	//注册
	$("#registerForm").validate({
		rules: {
			loginname: {
				required: true,
				isMobile: true,
				equalTo:"#phone",
			},
			phone: {
				required: true,
				isMobile: true,
			},
			vcode: {
				required: true,
			},	
			password: {
				required: true,
				minlength: 6,
			},
			password_confirm: {
				required: true,
				minlength: 6,
				equalTo:"#password",
			},
            agree: {
				required: true,
			},			
		},
		messages: {
			loginname: {
				required: "请填写手机号码",
				isMobile: "请填写正确手机号码",
				equalTo:"账号必须和手机号一致",
			},
			phone: {
				required: "请填写手机号码",
				isMobile: "请填写正确手机号码",
			},
			vcode: {
				required: "请填写短信验证码",
			},	
			password: {
				required: "请填写密码",
				minlength: "密码必须为6数以上",
			},
			password_confirm: {
				required: "请填写确认密码",
				minlength: "密码必须为6数以上",
				equalTo:"密码和确认码不一样",
			},	
            agree: {
				required: "请接受我们的协议",
			},				
		},
		submitHandler:function(form) {
          var url = baseUrl+'public/postregister';		  
          $.ajax({
  			url:url,
			type:'post',
			dataType:'json',
			data: $('#registerForm').serialize(),
			success:function(res){
				if(res.status==1){
			     	top.location.href = baseUrl+res.url;
				}else{
					layer.alert(res.info, {icon: 2});
				}
			}
          })
		}	
	});		
	
	//登录
	$("#loginForm").validate({
		rules: {
			phone: {
				required: true,
				minlength: 4,
			},
			password: {
				required: true,
				minlength: 6,
			},
			vcode: {
				required: true,
			},			
		},
		messages: {
			phone: {
				required: "请输入手机号或用户名",
				minlength: "用户名不能小于4位",
			},
			password: {
				required: "请输入密码",
				minlength: "密码不能小于6位",
			},
			vcode: {
				required: "请输入验证码",
			},				
		},
		submitHandler:function(form) {
          var url = baseUrl+'public/postlogin';		  
          $.ajax({
  			url:url,
			type:'post',
			dataType:'json',
			data: $('#loginForm').serialize(),
			success:function(res){
				if(res.status==1){
			     	top.location.href = baseUrl+res.url;
				}else{
					layer.alert(res.info, {icon: 2});
					if(form.getAttribute("callback")!=null)
					{
						eval(form.getAttribute("callback")+"()");
					}
					
				}
			}
          })
		}	
	});	
	
	
	// 手机号码验证
	jQuery.validator.addMethod("isMobile", function(value, element) {
		var length = value.length;
		var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
		return this.optional(element) || (length == 11 && mobile.test(value));
	}, "请正确填写您的手机号码");	
	
});	